package com.fliad.ai.controller;

import cn.hutool.core.util.IdUtil;
import com.fliad.ai.entity.AiAgent;
import com.fliad.ai.entity.AiLlm;
import com.fliad.ai.service.AiAgentService;
import com.fliad.ai.service.AiLlmService;
import lombok.extern.slf4j.Slf4j;
import org.noear.snack.ONode;
import org.noear.solon.ai.chat.ChatConfig;
import org.noear.solon.ai.chat.ChatModel;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.Result;
import org.noear.solon.core.util.MimeType;
import org.noear.solon.web.sse.SseEvent;
import reactor.core.publisher.Flux;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * @author wyl
 * @date 2025年07月10日 21:22
 */
@Slf4j
@Controller
public class AiAgentController {

    @Inject
    AiAgentService aiAgentService;
    @Inject
    AiLlmService aiLlmService;

    // 创建
    @Post
    @Mapping("")
    public Result<Boolean> create(@Body AiAgent agent) throws SQLException {
        agent.setId(IdUtil.getSnowflakeNextId());
        agent.setCreated(new Date());
        agent.setModified(new Date());
        return Result.succeed(aiAgentService.create(agent));
    }

    // 查询单个
    @Get
    @Mapping("/{id}")
    public Result<AiAgent> get(@Path("id") Long id) throws SQLException {
        AiAgent agent = aiAgentService.get(id);
        return Result.succeed(agent);
    }

    // 查询列表
    @Get
    @Mapping("")
    public Result<List<AiAgent>> list() throws SQLException {
        return Result.succeed(aiAgentService.list());
    }

    // 更新
    @Post
    @Mapping("/{id}")
    public Result<Boolean> update(@Path("id") Long id, @Body AiAgent agent) throws SQLException, IllegalAccessException {
        agent.setModified(new Date());
        return Result.succeed(aiAgentService.update(id, agent));
    }

    // 删除
    @Delete
    @Mapping("/{id}")
    public Result<Boolean> delete(@Path("id") Long id) throws SQLException {
        return Result.succeed(aiAgentService.delete(id));
    }

    @Post
    @Mapping("/chat/{id}")
    public void chat(@Path("id") Long id, ONode userMsg, Context ctx) throws SQLException {
        /*ChatModel chatModel = ChatModel.of("https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation")
                .apiKey("sk-1ffe449611a74e61ad8e71e1b35a9858")
                .provider("dashscope")
                .model("qwen-turbo-latest").build();*/
        AiAgent agent = aiAgentService.get(id);
        AiLlm llm = aiLlmService.get(agent.getLlmId());
        ChatConfig chatConfig = new ChatConfig();
        if (llm != null) {
            chatConfig.setApiKey(llm.getLlmApiKey());
            chatConfig.setApiUrl(llm.getLlmEndpoint());
            chatConfig.setProvider(llm.getDialect());
            chatConfig.setModel(llm.getLlmModel());
        }
        ChatModel.Builder chatModelBuilder = ChatModel.of(chatConfig);
        ChatModel chatModel = chatModelBuilder.build();

        ctx.contentType(MimeType.TEXT_EVENT_STREAM_VALUE);
        Flux.from(chatModel.prompt(userMsg.get("messages").get(0).get("content").getString()).stream())
                .filter(resp -> resp.hasChoices())
                .doOnNext(resp -> {
                    try {
                        ctx.render(resp.getMessage());
                        ctx.output("\n");
                        ctx.flush();
                    } catch (Throwable ex) {
                        throw new RuntimeException(ex);
                    }
                }).doOnError(err -> {
                    //
                }).then().block();
    }

}
